package src.src;
//package src;
import java.util.ArrayList;
import java.util.Hashtable;
import java.util.Iterator;
import java.util.List;

public class DoWork {

	String my_file;
	public DoWork(String the_file)
	{
        my_file = the_file;
        start();
	}
	
	private void start()
	{
		Hashtable table;
		SimpleGraph graph = new SimpleGraph();
		
		table = GraphInput.LoadSimpleGraph(graph, my_file);
		System.out.println(table.get(((Vertex)graph.vertexList.get(0))) + "8888888888888888888888888888888888888888888888888888888");
		System.out.println("edges " + graph.numEdges());
		System.out.println("vertices " + graph.numVertices());
		System.out.println("hash table crap    ");
		int total = 0;
		System.out.println("A minimum spanning tree of the specified graph may be composed of: ");
//		System.out.println(primMinSpanTree(graph).size());
		List<Edge> edges = primMinSpanTree(graph);
		
		for (int x = 0; x < edges.size(); x++) {
			Edge e = edges.get(x);
		if (e != null) {
			total ++;
			System.out.println("\tEdge "+ x + " " + ", going from " 
							   + e.getFirstEndpoint().getName() + " to " 
							   + e.getSecondEndpoint().getName());
		}
	}
//		for (Integer e: primMinSpanTree(graph)) {
//			if (e != null) {
//				total += (int) e.getData();
//				System.out.println("\tEdge " + e.getName() + ", going from " 
//								   + e.getFirstEndpoint().getName() + "to " 
//								   + e.getSecondEndpoint().getName());
//			}
//		}
		System.out.println("The total weight of the minimal spanning tree is " + total + " units");
	}
	
	private List<Edge> primMinSpanTree(SimpleGraph the_sg) {
		List<Edge> usedEdges = new ArrayList<Edge>();
		List<Vertex> knownVertices = new ArrayList<Vertex>();
		BinaryHeap primHeap = new BinaryHeap(the_sg.numEdges());
		Vertex initialV = the_sg.aVertex();
		knownVertices.add(initialV);
		primHeap.insert(((Edge)initialV.incidentEdgeList.get(0)).getNode());
		

	    while (!primHeap.isEmpty()) {
	    	try {
				Vertex current_vertex = ((PrimHeapNode) primHeap.deleteMin()).getVertex();
				
				for(int y = 0; y < current_vertex.incidentEdgeList.size(); y++)
			    {
				  if(!usedEdges.contains(current_vertex.incidentEdgeList.get(y)) && 
						  !knownVertices.contains(((Edge)(current_vertex.incidentEdgeList.get(y))).getSecondEndpoint()))
					  
				  {
				    primHeap.insert(((Edge) current_vertex.incidentEdgeList.get(y)).getNode());
				    usedEdges.add((Edge) current_vertex.incidentEdgeList.get(y));
				  }
			    }
				
			} catch (EmptyHeapException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
	    }
	    return usedEdges;
	    
//		
//		Iterator itr = the_sg.vertices();
//		PrimHeapNode[] nodearray = new PrimHeapNode[the_sg.numVertices()];
//		Vertex current;
//		int i = 0;
//		
//		while(itr.hasNext()) {
//			current = (Vertex)itr.next();
//			nodearray[i] = current.getNode();
//			i++;
//		}
//		
//		//BinaryHeap primHeap = new BinaryHeap();
//		BinaryHeap primHeap = new BinaryHeap(the_sg.numEdges());
//		primHeap.buildHeap(nodearray);
//        double current_small = (double) ((Edge) initialV.incidentEdgeList.get(0)).getData();
//        int small_index = 0;
//		for(int y = 0; y < initialV.incidentEdgeList.size(); y++)//Edge e :  initialV.getNode().getVertex().incidentEdgeList)
//		{
//		  if((double)(((Edge) initialV.incidentEdgeList.get(y)).getData()) < current_small)
//		  {
//			  current_small = (int)(((Edge) initialV.incidentEdgeList.get(y)).getData());
//			  small_index = y;
//		  }
//		}
////		System.out.println("edge chosen " + small_index);
////		System.out.println("*****************************************************************************************************");
//		usedEdges.add(((Edge) initialV.incidentEdgeList.remove(small_index)));
//		
//		
//		
//		PrimHeapNode currentNode;
//		Vertex currentV;
//		while (!primHeap.isEmpty()) {
//			try {
//				currentNode = (PrimHeapNode)primHeap.deleteMin();
//				currentV = currentNode.getVertex();
//				if(currentV.getNode().getVertex().incidentEdgeList.size() > 1)
//				{
//				  current_small = (double) ((Edge) currentV.incidentEdgeList.get(0)).getData();
//		          small_index = 0;
//				  for(int y = 0; y < currentV.incidentEdgeList.size(); y++)//Edge e :  initialV.getNode().getVertex().incidentEdgeList)
//			      {
//				    if((double)(((Edge) currentV.incidentEdgeList.get(y)).getData()) < current_small)
//				    {
//				  	  current_small = (double)(((Edge) currentV.incidentEdgeList.get(y)).getData());
//					  small_index = y;
//				    }
//				  }
//				  usedEdges.add(((Edge) currentV.incidentEdgeList.remove(small_index)));
//				}
//				else if(initialV.getNode().getVertex().incidentEdgeList.size() == 1)
//				{
//					usedEdges.add(((Edge) currentV.incidentEdgeList.get(0)));
//				}
//				knownVertices.add(currentV);
////				System.out.println("blah " + usedEdges.size());
//			} catch (EmptyHeapException e) {
//				e.printStackTrace();
//			}
//			
//		}
		
//		return usedEdges;
	}
	
}
